﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MetroFramework.Forms;
using System.IO;
using ComLib.ComType;
using LogLib;
using System.Net;

namespace FTPService_JG
{
    public partial class FormFTPOption : MetroForm
    {
        private FTP_JGOption _option = new FTP_JGOption();
        public FTP_JGOption Option
        {
            get { return _option; }
        }

        private void LoadSettings()
        {
            if (!File.Exists(_option.ConfigFilePath))
            {
                _option.Save();
            }
            else
            {
                _option.Load();
            }
        }

        public FormFTPOption()
        {
            LoadSettings();
            InitializeComponent();
        }

        private bool CheckInputting()
        {
            // 检查填写是否正确
            if (string.IsNullOrEmpty(textBoxIP.Text))
            {
                MessageBox.Show("IP地址为空.");
                return false;
            }
            if (string.IsNullOrEmpty(textBoxPort.Text))
            {
                MessageBox.Show("端口号为空.");
                return false;
            }
            if (string.IsNullOrEmpty(textBoxUser.Text))
            {
                MessageBox.Show("登录账户名称为空.");
                return false;
            }
            if (string.IsNullOrEmpty(textBoxPwd.Text))
            {
                MessageBox.Show("登录账户密码为空.");
                return false;
            }

            string ipStr = textBoxIP.Text;
            IPAddress ip;
            if (!IPAddress.TryParse(ipStr, out ip))
            {
                MessageBox.Show("IP地址填写不正确，请检查并重新输入.");
                return false;
            } 

            int iPort;
            if (!int.TryParse(textBoxPort.Text, out iPort))
            {
                MessageBox.Show("端口号填写不正确，请检查并重新输入");
                return false;
            }
            return true;
        }

        private void InitCtrls()
        {
            metroToggleNecessary.Checked = _option.IsNecessary;
            textBoxIP.Text = _option.IP;
            textBoxUser.Text = _option.User;
            textBoxPwd.Text = _option.GetPassword();
            textBoxFolder.Text = _option.Folder;
            textBoxPort.Text = _option.Port.ToString();
            metroComboBoxDestFileType.SelectedIndex = (int)_option.CopyOption;
            metroComboBoxDateType.SelectedIndex = (int)_option.DirFormat;
            metroComboBoxEncryptMode.SelectedIndex = (int)_option.EncryptionMode;
            metroToggleIsVerify.Checked = _option.NeedVerify;
        }

        private void UpdateData()
        {           
            _option.IsNecessary = metroToggleNecessary.Checked;
            _option.IP = textBoxIP.Text;
            _option.User = textBoxUser.Text;
            _option.MakeCiphertext(textBoxPwd.Text);
            _option.Folder = textBoxFolder.Text;
            _option.DirFormat = (DirectoryFormat)metroComboBoxDateType.SelectedIndex;
            _option.EncryptionMode = (FTPEncryptionMode)metroComboBoxEncryptMode.SelectedIndex;
            _option.NeedVerify = metroToggleIsVerify.Checked;
            _option.Port = int.Parse(textBoxPort.Text);          
            _option.CopyOption = (CopyOptions)metroComboBoxDestFileType.SelectedIndex;
            _option.Save();
        }

        private void buttonCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = System.Windows.Forms.DialogResult.Cancel;
            this.Close();
        }

        private void buttonOK_Click(object sender, EventArgs e)
        {
            if (!CheckInputting())
            {
                return;
            }
            UpdateData();
            this.DialogResult = System.Windows.Forms.DialogResult.OK;
            this.Close();
        }

        private void FormFTPOption_Load(object sender, EventArgs e)
        {
            InitCtrls();
        }
    }
}
